package cn.bdqn.controller.question;



import cn.bdqn.pojo.question.Message;
import cn.bdqn.pojo.question.Question;
import cn.bdqn.service.question.QuestionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/question")
@CrossOrigin()
public class QuestionController {
    @Autowired
    private QuestionService questionService;
    @PreAuthorize("hasAuthority('question:management')")
    @GetMapping("/list/{pageNum}/{pageSize}")
    public Message getAll(@PathVariable Integer pageNum, @PathVariable Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List data = questionService.list();
        Message message = new Message();
        message.setData(data);
        PageInfo<Question> page= new PageInfo<>(data);
        message.setPage(page);
        return message;
    }
    // 添加
    @PreAuthorize("hasAuthority('question:management')")
    @PostMapping("/add")
    public boolean save(@RequestBody Question question){
        return questionService.save(question) ;
    }

    // 更新
    @PreAuthorize("hasAuthority('question:management')")
    @PutMapping("/update")
    public boolean update(@RequestBody Question question){
        return questionService.updateById(question);
    }
    // 删除byId
    @PreAuthorize("hasAuthority('question:management')")
    @DeleteMapping("/delete/{id}")
    public boolean delete(@PathVariable Integer id){
        return  questionService.removeById(id);
    }
    // 查询单个
    @PreAuthorize("hasAuthority('question:management')")
    @GetMapping("/single/{id}")
    public Question getById(@PathVariable Integer id){
        return questionService.getById(id);
    }
    @PreAuthorize("hasAuthority('question:management')")
    @GetMapping("/like")
    public List<Question> getLike(@RequestParam("queryName") String name){
        System.out.println(name);
        QueryWrapper<Question> lq = new QueryWrapper<>();
        lq.like("question_title",name);
        return questionService.list(lq);
    }

}
